/*
 * UCache utility methods and functionality
 * Copyright (C) 2013 
 */
#include "internal.h"
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <openssl/sha.h>

u64
hashpath(const char* path)
{
	size_t len;
	u64 trnchash;
        char respath[PATH_MAX];
	char* restest;
	unsigned char fullhash[SHA_DIGEST_LENGTH];

	/*
	 * We we're handed a path that was given to ucache, and
	 * we're going to allocate an inode number based on 
	 * that requested path. The problem can come in the form
	 * of relative paths, so we need to expand them so that
	 * we don't get weird duplicates.
	 */
	restest = realpath(path, respath);
	if(restest == NULL)
		goto fail;

	len = strlen(respath);
	SHA1((unsigned char const*)respath, len, fullhash);

	/*
	 * A little bit of a hack, but we only want the first 
	 * 64 bits as our 'hash'. It's not crypto-secure, but 
	 * _should_ provide us enough uniqueness as to not get 
	 * any collisions for the forseeable future.
	 */
	trnchash = ((u64*)fullhash)[0];
	return trnchash;
fail:
	return 0;
}
